library(kernlab)
library(caret)

setwd("D:/ClassificationR")

#variables
a_accuracy = 0
a_sensitivity_setosa = 0
a_specificity_setosa = 0
a_sensitivity_versicolor = 0
a_specificity_versicolor = 0
a_sensitivity_virginica = 0
a_specificity_virginica = 0

for(i in 1:5) {
  #membaca data training & data testing
  data.training = read.csv(paste0("iris.train.",i,".csv"))
  data.test = read.csv(paste0("iris.test.",i,".csv"))
  
  #membuat model & melakukan prediksi 
  model = ksvm(Species~., data.training, type="spoc-svc")
  
  #melakukan prediksi 
  predict_result = predict(model, data.test[,-5])
  
  #menghitung kinerja
  performance.value = confusionMatrix(predict_result, data.test[,5])
  print("------------------------------------------------")
  print(performance.value)
  
  #menghitung jumlah setiap nilai kinerja
  a_accuracy = a_accuracy + performance.value$overall[1]
  a_sensitivity_setosa = a_sensitivity_setosa + performance.value$byClass[1]
  a_specificity_setosa = a_specificity_setosa + performance.value$byClass[4]
  
  a_sensitivity_versicolor = a_sensitivity_versicolor + performance.value$byClass[2]
  a_specificity_versicolor = a_specificity_versicolor + performance.value$byClass[5]
  
  a_sensitivity_virginica = a_sensitivity_virginica + performance.value$byClass[3]
  a_specificity_virginica = a_specificity_virginica + performance.value$byClass[6]
}

print(paste("average accuracy:", a_accuracy/i))
print(paste("average sensitivity - setosa:", a_sensitivity_setosa/i))
print(paste("average specificity - setosa:", a_specificity_setosa/i))
print(paste("average sensitivity - versicolor:", a_sensitivity_versicolor/i))
print(paste("average specificity - versicolor:", a_specificity_versicolor/i))
print(paste("average sensitivity - virginica:", a_sensitivity_virginica/i))
print(paste("average specificity - virginica:", a_specificity_virginica/i))
